package cn.sdormitory.smartclass.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.sdormitory.smartclass.dao.ClarepairDeviceDao;
import cn.sdormitory.smartclass.entity.ClarepairDevice;
import cn.sdormitory.smartclass.service.ClassRepairDeviceService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @创建人：zhouyang
 * @创建时间：2021/12/22 22:51
 * @version：V1.0
 */
@Slf4j
@Service("classRepairDeviceServiceImpl")
public class ClassRepairDeviceServiceImpl extends ServiceImpl<ClarepairDeviceDao, ClarepairDevice> implements ClassRepairDeviceService {
    @Override
    public List<Long> listDeviceIdByRepairId(Long repairId) {
        List<Long> ids = new ArrayList<>();
        List<ClarepairDevice> repairDevices = this.baseMapper.selectList(new LambdaQueryWrapper<ClarepairDevice>().eq(ClarepairDevice::getClassRepairId, repairId));
        if (CollectionUtil.isNotEmpty(repairDevices)) {
            repairDevices.forEach(item -> {
                ids.add(item.getClassDeviceId());
            });
            return ids;
        }
        return null;
    }

    @Override
    public void insertRepairAndRepairDevice(Long repairId, List<Long> deviceIdList) {
        this.baseMapper.insertRepairAndRepairDevice(repairId,deviceIdList);
    }

    @Override
    public void delAndCreateDevice(Long repairId, List<Long> deviceIdList) {
        remove(new LambdaQueryWrapper<ClarepairDevice>().eq(ClarepairDevice::getClassRepairId, repairId));
        if (CollUtil.isNotEmpty(deviceIdList)) {
            //保存报修与设备关系
            insertRepairAndRepairDevice(repairId, deviceIdList);
        }
    }
}
